{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "931b2806",
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_generator():\n",
    "    print(\"Start\")\n",
    "    yield 1\n",
    "    print(\"Middle\")\n",
    "    yield 2\n",
    "    print(\"End\")\n",
    "    yield 3\n",
    "\n",
    "# 使用生成器\n",
    "gen = my_generator()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88688b03",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(next(gen))  # 输出 Start 然后 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "839237df",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(next(gen))  # 输出 Middle 然后 2\n",
    "print(next(gen))  # 输出 End 然后 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0f888fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fibonacci(n):\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        yield a\n",
    "        a, b = b, a + b\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6d25622",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用生成器\n",
    "for number in fibonacci(100):\n",
    "    print(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e6e800e",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares = (x * x for x in range(5))\n",
    "\n",
    "for num in squares:\n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0264c599",
   "metadata": {},
   "outputs": [],
   "source": [
    "(i*i for i in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8e66537",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(i*i for i in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d4798c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum((i*i for i in range(10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c749c4fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "page = [\n",
    "    \"hello world\",\n",
    "    \"hello python world\",\n",
    "    \"hello data science\"\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d9b0241",
   "metadata": {},
   "outputs": [],
   "source": [
    "page"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b9c82e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "unique_words = set(word for line in page for word in line.split())\n",
    "unique_words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa332d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "list(word for line in page)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c35342d",
   "metadata": {},
   "outputs": [],
   "source": [
    "unique_words = set()\n",
    "for line in page:\n",
    "    for word in line.split():\n",
    "        unique_words.add(word)\n",
    "unique_words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6254c47e",
   "metadata": {},
   "outputs": [],
   "source": [
    "(word for line in page for word in line.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8dd808e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student:\n",
    "    def __init__(self, name, gpa):\n",
    "        self.name = name\n",
    "        self.gpa = gpa\n",
    "\n",
    "graduates = [\n",
    "    Student(\"Alice\", 3.8),\n",
    "    Student(\"Bob\", 3.9),\n",
    "    Student(\"Charlie\", 3.7)\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce25dba4",
   "metadata": {},
   "outputs": [],
   "source": [
    "valedictorian = max((student.gpa, student.name) for student in graduates)\n",
    "valedictorian"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6e00d59",
   "metadata": {},
   "outputs": [],
   "source": [
    "((student.gpa, student.name) for student in graduates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a511fa37",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "def infinite_counter():\n",
    "    i = 0\n",
    "    while True:\n",
    "        yield i\n",
    "        i += 1\n",
    "        time.sleep(1)\n",
    "\n",
    "# 使用生成器模拟每秒计数\n",
    "counter = infinite_counter()\n",
    "for _ in range(5):\n",
    "    print(next(counter))\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
